package com.accp.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.accp.pojo.Goods;
import com.accp.pojo.Income;
import com.accp.pojo.OrderInfo;
import com.accp.pojo.Room;
import com.accp.pojo.RoomType;
import com.accp.pojo.Vip;
import com.accp.service.OrderInfoService;
import com.accp.utils.DateUtil;

@Controller
public class OrderController {

	@Autowired
	private OrderInfoService orderInfoService;
	/*
	 * 张智琛
	 * 所有方法在一个控制层
	 */
	
	//7位随机数
	public int random(){
		int num=(int)((Math.random()*9+1)*1000000);
		
		return num;
	}
	
	//代替登录方法
	@RequestMapping("/orderList")
	public String login(Integer roomTypeId, Integer jiezhang,
			String beginTime, String overTime, Integer roomNum, String user,HttpServletRequest request,HttpServletResponse response){
		System.out.println("登录方法");
		List<OrderInfo> list=orderInfoService.getAllOrder(roomTypeId, jiezhang, beginTime, overTime, roomNum, user);//获取查询列表
		List<RoomType> roomtype=orderInfoService.getRoomType();
		request.setAttribute("list", list);//返回查询总订单
		request.setAttribute("roomtype", roomtype);
		
		return "orderList";
//		for (OrderInfo orderInfo : list) {
//			 
//			System.out.println(orderInfo.getOrdercreatetime());
//		}
//		return null;
	}
	
	//订单详细信息
	@RequestMapping("/orderdetails")
	public String getbyid(Integer id,HttpServletRequest request,HttpServletResponse response){
		System.out.println("执行ID查询操作 id="+id);
		OrderInfo orderinfo=orderInfoService.getByOrderId(id);//订单信息
		Income income=orderInfoService.getIncomeByOrderId(id);//订单消费信息
		request.setAttribute("orderinfo",orderinfo);
		if(income!=null){//存在消费信息，已结账，显示结账金额
			request.setAttribute("income", income);
		}
		return "orderdetails";
	}
	
	
	/*
	 * 结账前操作
	 * */
	@RequestMapping("/slmt")
	public String settlement(Integer id,HttpServletRequest request,HttpServletResponse response){
		System.out.println("执行结算操作");
		OrderInfo orderinfo=orderInfoService.getByOrderId(id);//订单信息
		List<RoomType> roomtype=orderInfoService.getRoomType();//全部房间信息查询
		List<Goods> goods=orderInfoService.getAllGoods();//全部物品信息
		List<Vip> vip=orderInfoService.getVipAll();//vip信息
		int timepoor=DateUtil.getDatePoor(DateUtil.convertStringToDate(DateUtil.getNow()), DateUtil.convertStringToDate(orderinfo.getOrdercreatetime()));//时间差计算
		request.setAttribute("orderinfo",orderinfo);
		request.setAttribute("roomtype",roomtype);
		request.setAttribute("goods",goods);
		request.setAttribute("vip",vip);
		request.setAttribute("nowtime",DateUtil.getNow());//获取当前时间
		request.setAttribute("timepoor",timepoor);//时间差
		System.out.println(orderinfo.getResidents().getVipid());//CES
		
		return "Settlement";
		//return null;
	}
	
	
	
	@RequestMapping("/updateOrder")
	public String updateOrder(){
		System.out.println("执行修改操作");
		
		return "orderUpdate";
	}
	
	
	//添加前操作
	@RequestMapping("/addorder")
	public String addOrder(HttpServletRequest request,HttpServletResponse response){
		System.out.println("执行添加前操作");
		List<RoomType> roomtype=orderInfoService.getRoomType();//全部房间类型
		List<Room> room=orderInfoService.getallroom();//未住全部房间
		List<Vip> vip=orderInfoService.getVipAll();//vip
		
		request.setAttribute("roomtype",roomtype);
		request.setAttribute("room",room);
		request.setAttribute("vip",vip);
		return "addOrder";
	}
	
	//添加订单操作
	@RequestMapping("/addlast")
	public String addallorder(Integer roomid,String name,String idcard,int vipid){
		
//		System.out.println(roomid);
//		System.out.println(name);
//		System.out.println(idcard);
//		System.out.println(vipid);
//		System.out.println(random());
		//添加用户信息
		if(orderInfoService.quertmanifexi(idcard)!=1){//如果存在用户不添加
			orderInfoService.newResidents(idcard, vipid, name);
		}
		//添加订单信息
		orderInfoService.neworder(random(), idcard, roomid);
		//房间信息更改
		orderInfoService.openroom(roomid);
		//
		return "redirect:orderList";
	}
	
	@RequestMapping("/jiezhang")
	public String jiezhang(Integer oid,Integer roomid,double money,Integer[] gods){
//		System.out.println("执行结账操作");
//		System.out.println(oid);
//		System.out.println(roomid);
		System.out.println(money);
		for (Integer i : gods) {
			System.out.println(i);
		}
		orderInfoService.jiesuanorder(oid);//结算订单
		//修改为空房
		orderInfoService.jeisuanroom(roomid);
		//添加收入表
		orderInfoService.addincome(oid, money);
		//使用物品添加
		orderInfoService.addGoods(oid, gods);
		return "redirect:orderList";
		//return null;
	}
}
